package com.hm.PushCaseService.service.timestamp;

import com.hm.PushCaseService.mapper.timestamp.LastUpdateTimeMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 时间戳服务
 */

@Service
public class CaseLastUpdateTimeService implements ICaseLastUpdateTime {

    //根据这个key查询最后更新时间
    private static final String QueryKey = "1";

    @Qualifier("MainSqlSessionFactory")
    public final SqlSessionFactory sqlSessionFactory;

    public CaseLastUpdateTimeService(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public String getTime() {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            LastUpdateTimeMapper mapper = session.getMapper(LastUpdateTimeMapper.class);
            Map<String, String> params = new HashMap<>();
            params.put("queryId", QueryKey);
            return mapper.getTime(params).lastUpdateTime;
        }
    }

    @Override
    public void updateTime(String currentTime) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            LastUpdateTimeMapper mapper = session.getMapper(LastUpdateTimeMapper.class);
            Map<String, String> currentTimeParams = new HashMap<>();
            currentTimeParams.put("id", "1");
            currentTimeParams.put("queryKey", QueryKey);
            currentTimeParams.put("curryTime", currentTime);
            mapper.updateTime(currentTimeParams);
        }
    }
}
